package basic.nio;

import org.junit.Test;

import javax.xml.ws.RequestWrapper;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class ChanelTest {

    /**
     *  通道之间的数据传输(直接缓冲区)
     * */
    @Test
    public void test3(){

        try {
            FileChannel inchanel = FileChannel.open(Paths.get("demo/1.png"),StandardOpenOption.READ);
            FileChannel outChanel = FileChannel.open(Paths.get("demo/4.png"),StandardOpenOption.WRITE,StandardOpenOption.CREATE);
            inchanel.transferTo(0,inchanel.size(),outChanel);
            inchanel.close();
            outChanel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     *  直接缓冲区
     * */
    @Test
    public void test2(){
        try {
            FileChannel inchanel = FileChannel.open(Paths.get("demo/1.png"),StandardOpenOption.READ);
            FileChannel outChanel = FileChannel.open(Paths.get("demo/3.png"),StandardOpenOption.WRITE,StandardOpenOption.READ,StandardOpenOption.CREATE);
            MappedByteBuffer in = inchanel.map(FileChannel.MapMode.READ_ONLY,0,inchanel.size());
            MappedByteBuffer out = outChanel.map(FileChannel.MapMode.READ_WRITE,0,inchanel.size());
            byte[] bys = new byte[in.limit()];
            in.get(bys);
            out.put(bys);
            inchanel.close();
            outChanel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /***
     *非直接缓冲区
     * */
    @Test
    public void test1()  {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        FileChannel in = null;
        FileChannel out = null;
        try {
             fis = new FileInputStream(new File("demo/1.png"));
             fos = new FileOutputStream(new File("demo/chanel1.png"));
             in = fis.getChannel();
             out = fos.getChannel();
            ByteBuffer bys = ByteBuffer.allocate(1024);
            while (in.read(bys)!=-1){
                bys.flip();
                out.write(bys);
                bys.clear();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fis.close();
                fos.close();
                in.close();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
    @Test
    public void test4() throws IOException {
        RandomAccessFile fromFile = new RandomAccessFile("demo/b.txt","rw");
        FileChannel fromchannel = fromFile.getChannel();
        RandomAccessFile toFile = new RandomAccessFile("demo/b-b.txt","rw");
        FileChannel toChannel = toFile.getChannel();
        long  position  = 0l;
        long size = fromchannel.size();
        toChannel.transferTo(position,size,fromchannel);

    }

    @Test
    public void test5(){
        boolean a=true;
        boolean b = false;
        System.out.println(a&&b);

    }

}
